package com.mo.tools.sequence.service.impl;

import com.mo.tools.sequence.FileSequence;
import com.mo.tools.sequence.mapper.FileSequenceMapper;
import com.mo.tools.sequence.service.ISequenceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

/**
 * @name: SequenceServiceImpl
 * @description: 序列逻辑处理对象
 * @author: Yang Xingfu
 * @date: 2019-12-19 08:53
 **/

@Service
@Slf4j
//@Transactional(rollbackFor = {Exception.class})
public class SequenceServiceImpl implements ISequenceService {

    /**
     * 序列数据处理对象
     */
//    @Autowired
    private FileSequenceMapper sequenceMapper;


    @Override
    public FileSequence initializeFind(FileSequence sequence) {

        FileSequence sequenceResult = null;
        final String INITIALIZE_SEQUENCE = "0";

        try {
//            QueryWrapper<FileSequence> query = new QueryWrapper<>(sequence);
//            sequenceResult = sequenceMapper.selectOne(query);

            if (sequenceResult == null) {
                Date date = new Date();
                sequence.setId(UUID.randomUUID().toString());
                sequence.setSequence(INITIALIZE_SEQUENCE);
                sequence.setCreateTime(date);
                sequence.setUpdateTime(date);
                sequenceMapper.insertSelective(sequence);
                sequenceResult = sequenceMapper.selectByPrimaryKey(sequence.getId());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sequenceResult;
    }

    @Override
    public void update(FileSequence sequence) {
        try {
            Date date = new Date();
            sequence.setCreateTime(date);
            sequence.setUpdateTime(date);
            sequenceMapper.updateByPrimaryKeySelective(sequence);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
